package com.magic.gre.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.net.RxBus;

import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI;

/**
 * Author: Lmoumou
 * Date: 2019-01-29 18:00
 * 文件名: NetworkChangeReceiver
 * 描述:
 */
public class NetworkChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectionManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        MsgEvent msgEvent=new MsgEvent(MsgEvent.NET_STATUS);
        if (networkInfo != null && networkInfo.isAvailable()) {
            switch (networkInfo.getType()) {
                case TYPE_MOBILE:
                   msgEvent.put("status",1);
                    break;
                case TYPE_WIFI:
                    msgEvent.put("status",2);
                    break;
                default:
                    break;
            }
        } else {
            msgEvent.put("status",0);
        }

        RxBus.getInstance().post(msgEvent);
    }
}
